//
//  CommentListVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/21.
//  Copyright © 2016年 阳光. All rights reserved.
//  评论列表界面

#import "CommentListVC.h"
#import "CommentListCell.h"

static NSString *commentListCellIdentifier=@"CommentListCellIdentifier";

@interface CommentListVC ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *commentListTableView;

@property(nonatomic,strong)NSMutableArray *commentListArr;

@end

@implementation CommentListVC

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.title=@"评论列表";
    
    _commentListArr=[NSMutableArray array];
    
    [self initNavBarBtnItem];
    
    [self initTableView];
    
    [self setMJRefreshTableView];
    
}


-(void)initTableView
{
    // _commentListTableView
    _commentListTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,1,APPW,APPH-49) style:UITableViewStylePlain];
    _commentListTableView.delegate=self;
    _commentListTableView.dataSource=self;
    _commentListTableView.backgroundColor=MAIN_BACK_COLOR;
    _commentListTableView.showsVerticalScrollIndicator=NO;
    _commentListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_commentListTableView];
    [_commentListTableView registerClass:[CommentListCell class] forCellReuseIdentifier:commentListCellIdentifier];
}


-(void)setMJRefreshTableView
{
    _commentListTableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_commentListTableView.mj_header beginRefreshing];
}


-(void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
        [self getCommentList];
        dispatch_async(dispatch_get_main_queue(), ^{
            //update UI
        });
    });
}


-(void)getCommentList
{
    NSDictionary *dic=@{
                        @"doctor":[Util getUserDefaultObjectForKey:@"id"],
                        @"id":_paperID,
                        @"type_id":@(0),
                        @"count":@"20",
                        @"page":@"1"
                        };
    
     __weak typeof(self) weakSelf = self;
    [QBRequest CommentListWithDic:dic Success:^(id res)
     {
         if ([res[@"info"]count]>0)
         {
             weakSelf.commentListArr=[CommentListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];
         }
         else
         {
              [self.view makeToast:@"暂无评论" duration:1.0 position:CSToastPositionCenter];
         }
         
         [weakSelf.commentListTableView reloadData];
         [weakSelf.commentListTableView.mj_header endRefreshing];
         
    } failure:^(NSString *error)
     {
        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentListArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentListCell *cell=[tableView dequeueReusableCellWithIdentifier:commentListCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.commentListModel=self.commentListArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kmargin*2;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}


-(void)initNavBarBtnItem
{
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem initWithNormalImage:@"back_gray" target:self action:@selector(backItemEvent)];
}

-(void)backItemEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

@end
